<?php

namespace StudyBuddy;

/**
 * Class for getting parsed html
 * of user questions block
 */
class UserQuestions extends StudyBuddyObject {
    const PER_PAGE = 10;

    /**
     *
     * Get parsed html of user questions div,
     * complete with pagination
     *
     * @todo there has to be an extra request param "tab" and ONLY if
     * it equals to 'questions' then it would mean
     * that sort and pagination is for this block because it could
     * be for 'answer' block since they both come to the same controller
     * OR just make separate controllers and then pagination and sorting
     * will work ONLY with AJAX  and then just hide pagination from
     * non-js browsers!
     *
     * @param Registry $oRegistry
     * @param User $oUser
     *
     * @return string html of user questions
     */
    public static function get(Registry $oRegistry, User $oUser) {
        $uid = $oUser->getUid();
        if (0 === $uid) {
            d('not registered user');

            return '';
        }

        $pagerLinks = '';

        $pageID = $oRegistry->Request->get('pageID', 'i', 1);
        /**
         * Default pager path
         */
        $pagerPath = '/tab/q/' . $uid . '/recent';
        /**
         * Default sort condition
         *
         * @var array
         */
        $sort = array('i_ts' => 1);

        //$mode = $oRegistry->Request->get('tab', 's', '');

        /**
         * sort order possible values:
         * recent, oldest, voted, updated, views
         *
         * default is oldest first
         *
         * $mode 'questions' means that user requested
         * either pagination or sorting specifically
         * for the User Questions
         * This is not necessaraly an ajax request as pagination
         * will work without Ajax too.
         *
         * When there is no explicit 'questions' $mode
         * then we need to just get the cursor
         * with default sort and pageID, treating this
         * request as if it was for pageID 1 and sort=oldest
         */
        //if('q' === $mode){

        $cond = $oRegistry->Request->get('sort', 's', 'recent');
        switch ($cond) {
            case 'recent':
                $sort = array('i_ts' => -1);
                $pagerPath = '/tab/q/' . $uid . '/recent';
                break;

            case 'voted':
                $sort = array('i_votes' => -1);
                $pagerPath = '/tab/q/' . $uid . '/voted';
                break;

            case 'updated':
                $sort = array('i_etag' => -1);
                $pagerPath = '/tab/q/' . $uid . '/updated';
                break;

            case 'views':
                $sort = array('i_views' => -1);
                $pagerPath = '/tab/q/' . $uid . '/views';
                break;

            default:
                $sort = array('i_ts' => 1);
                $pagerPath = '/tab/q/' . $uid . '/oldest';
                break;
        }
        //}

        $cursor = self::getCursor($oRegistry, $uid, $sort);
        $count = $cursor->count(true);
        d('$count: ' . $count);

        /**
         * If this user does not have any questions then return
         * empty string
         */
        if (0 == $count) {
            d('no user questions');
            return '';
        }

        if ($count > self::PER_PAGE || $pageID > 1) {

            $oPaginator = Paginator::factory($oRegistry);

            $oPaginator->paginate($cursor, self::PER_PAGE, array('path' => $pagerPath));

            $pagerLinks = $oPaginator->getLinks();
            d('links: ' . $pagerLinks);
        }


        $questions = \tplUquestions::loop($cursor);
        d('questions: ' . $questions);

        $vals = array(
            'count' => $count,
            'questions' => $questions,
            'pagination' => $pagerLinks);

        return \tplUserQuestions::parse($vals);
    }

    protected static function getCursor(Registry $oRegistry, $uid, array $sort) {

        $where = array('i_uid' => $uid);
        /**
         * Exclude deleted items unless viewer
         * is a moderator
         */
        if (!$oRegistry->Viewer->isModerator()) {
            $where['i_del_ts'] = null;
        }

        $cursor = $oRegistry->Mongo->QUESTIONS->find($where)->sort($sort);

        return $cursor;
    }

}
